package controllers;

import java.io.IOException;
import java.sql.SQLException;

import models.BeanUser;
import utils.BeanUtilities;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class formcontroller
 */
public class formcontroller extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public formcontroller() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BeanUser user = new BeanUser();
		BeanUtilities.populateBean(user, request);
	    if (user.isComplete() && user.isValid()) {
	    	try {
	    		DAO dao = new DAO();
				dao.register(user.getAlias(), user.getPass(), user.getMail());
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
	    	RequestDispatcher dispatcher = request.getRequestDispatcher("registration_2.jsp");
	    	if (dispatcher != null) dispatcher.forward(request, response);
	    } else {
	    	request.setAttribute("user",user);
	    	RequestDispatcher dispatcher = request.getRequestDispatcher("registration_1.jsp");
	    	if (dispatcher != null) dispatcher.forward(request, response);
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
